Coverage Report

Created: 2024-12-19 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\file\core.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::gen::file::Content;
30
use bp3d_util::path::PathExt;
31
use std::borrow::Cow;
32
use std::fmt::{Debug, Formatter};
33
use std::io::Write;
34
use std::path::{Path, PathBuf};
35
36
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
37
pub enum FileType {
38
    MessageWriting,
39
    MessageReading,
40
    Message,
41
    Structure,
42
    Enum,
43
    Union,
44
}
45
46
pub struct File {
47
    name: Cow<'static, str>,
48
    data: Option<String>,
49
    ty: FileType,
50
}
51
52
impl Debug for File {
53
211
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54
211
        write!(f, "File {{ name: {:?}, ty: {:?} }}", self.name, self.ty)
55
211
    }
56
}
57
58
impl File {
59
211
    pub fn new(ty: FileType, name: impl Into<Cow<'static, str>>, data: &Content) -> Self {
60
211
        Self {
61
211
            name: name.into(),
62
211
            ty,
63
211
            data: data.to_string(),
64
211
        }
65
211
    }
66
67
211
    pub fn ty(&self) -> FileType {
68
211
        self.ty
69
211
    }
70
71
209
    pub fn write(
72
209
        self,
73
209
        out_directory: &Path,
74
209
        file_header: Option<&str>,
75
209
        extension: &str,
76
209
    ) -> std::io::Result<Option<PathBuf>> {
77
209
        if let Some(
data89
) = self.data {
  Branch (77:16): [True: 89, False: 120]
  Branch (77:16): [Folded - Ignored]
78
89
            if data.len() <= 1 {
  Branch (78:16): [True: 0, False: 89]
  Branch (78:16): [Folded - Ignored]
79
0
                return Ok(None);
80
89
            }
81
89
            let sub_folder = self.name.find("/").map(|id| 
&self.name[..id]0
);
82
89
            if let Some(
sub_folder0
) = sub_folder {
  Branch (82:20): [True: 0, False: 89]
  Branch (82:20): [Folded - Ignored]
83
0
                std::fs::create_dir(out_directory.join(sub_folder))?;
84
89
            }
85
89
            let path = out_directory.join(&*self.name).ensure_extension(extension).to_path_buf();
86
89
            let mut file = std::fs::File::create(&path)
?0
;
87
89
            if let Some(
file_header0
) = file_header {
  Branch (87:20): [True: 0, False: 89]
  Branch (87:20): [Folded - Ignored]
88
0
                file.write_all(file_header.as_bytes())?;
89
89
            }
90
89
            file.write_all(data.as_bytes())
?0
;
91
89
            file.flush()
?0
;
92
89
            drop(file);
93
89
            Ok(Some(path))
94
        } else {
95
120
            Ok(None)
96
        }
97
209
    }
98
}